using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Plugin.Sns.Scene;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MicroMsg.Manager
{
	public class SnsAsyncMgr
	{
		private const int max_error_retry_times = 10;
		private static NetSceneSnsObjectOp sceneOp;
		private static List<SnsOpLog> curOpList = new List<SnsOpLog>();
		private static SnsAsyncQueue queue;
		public static void init()
		{
			AccountMgr.registerLoginNotify(new onAccountLoginCallback(SnsAsyncMgr.onLogin));
			EventCenter.registerEventHandler(EventConst.ON_NETSCENE_SYNC, new EventHandlerDelegate(SnsAsyncMgr.onSyncEnd));
		}
		private static void onLogin()
		{
			if (SnsAsyncMgr.sceneOp != null)
			{
				SnsAsyncMgr.sceneOp.cancel();
			}
			SnsAsyncMgr.sceneOp = new NetSceneSnsObjectOp(new NetSceneSnsObjectOpCallBack(SnsAsyncMgr.onSendSnsOpLogRsp));
			SnsAsyncMgr.curOpList.Clear();
			SnsAsyncMgr.queue = ConfigMgr.read<SnsAsyncQueue>();
			if (SnsAsyncMgr.queue == null)
			{
				SnsAsyncMgr.queue = new SnsAsyncQueue();
			}
		}
		private static void onSyncEnd(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			NetSceneSyncResult netSceneSyncResult = evtArgs.mObject as NetSceneSyncResult;
			if (netSceneSyncResult == null)
			{
				return;
			}
			if (netSceneSyncResult.syncStatus == SyncStatus.syncEnd && netSceneSyncResult.isAppActive)
			{
				SnsAsyncMgr.trySendSnsOpLog();
			}
		}
		private static SnsOpLog addSnsOp(ulong snsObjectID, int opType, int updateSnsObj = 0, int delCommentID = 0)
		{
			SnsOpLog snsOpLog = Enumerable.FirstOrDefault<SnsOpLog>(Enumerable.Where<SnsOpLog>(SnsAsyncMgr.queue.OpList, (SnsOpLog snsOpLog_0) => snsOpLog_0.nObjectID == snsObjectID && snsOpLog_0.nOpType == opType && snsOpLog_0.nDelCommentId == delCommentID));
			if (snsOpLog != null)
			{
				return snsOpLog;
			}
			snsOpLog = new SnsOpLog(snsObjectID, opType, updateSnsObj, delCommentID);
			SnsAsyncMgr.queue.OpList.Add(snsOpLog);
			if (opType == 1 && !SnsAsyncMgr.queue.delList.Contains(snsObjectID))
			{
				SnsAsyncMgr.queue.delList.Add(snsObjectID);
			}
			ConfigMgr.delayWrite<SnsAsyncQueue>(SnsAsyncMgr.queue);
			return snsOpLog;
		}
		public static void delete(ulong snsObjectID, int updateSnsObj = 0)
		{
			SnsAsyncMgr.addSnsOp(snsObjectID, 1, updateSnsObj, 0);
			SnsPageMgr.notifySnsDelete();
			SnsAsyncMgr.trySendSnsOpLog();
		}
		public static void setPrivacy(ulong snsObjectID, bool bPrivacy, int updateSnsObj = 0)
		{
			SnsAsyncMgr.addSnsOp(snsObjectID, bPrivacy ? 2 : 3, updateSnsObj, 0);
			SnsAsyncMgr.trySendSnsOpLog();
		}
		public static void delComment(ulong snsObjectID, int commentID, int updateSnsObj = 0)
		{
			SnsAsyncMgr.addSnsOp(snsObjectID, 4, updateSnsObj, commentID);
			SnsAsyncMgr.trySendSnsOpLog();
		}
		public static void setLikeFlag(ulong snsObjectID, bool bLike, int updateSnsObj = 0)
		{
			if (bLike)
			{
				return;
			}
			SnsAsyncMgr.addSnsOp(snsObjectID, 5, updateSnsObj, 0);
			SnsAsyncMgr.trySendSnsOpLog();
		}
		public static List<ulong> getDeletingIDS()
		{
			return Enumerable.ToList<ulong>(Enumerable.Select<SnsOpLog, ulong>(Enumerable.Where<SnsOpLog>(SnsAsyncMgr.queue.OpList, (SnsOpLog snsOpLog_0) => snsOpLog_0.nOpType == 1), (SnsOpLog snsOpLog_0) => snsOpLog_0.nObjectID));
		}
		public static List<ulong> getDeleteIDS()
		{
			return SnsAsyncMgr.queue.delList;
		}
		public static bool isDeletedObjectID(ulong ojbectID)
		{
			return SnsAsyncMgr.queue.delList.Contains(ojbectID);
		}
		public static void removeDeleteIDS(List<ulong> list_0)
		{
			SnsAsyncMgr.queue.delList = Enumerable.ToList<ulong>(Enumerable.Except<ulong>(SnsAsyncMgr.queue.delList, list_0));
			ConfigMgr.delayWrite<SnsAsyncQueue>(SnsAsyncMgr.queue);
		}
		public static void ClearAll()
		{
			SnsAsyncMgr.queue.delList.Clear();
			ConfigMgr.delayWrite<SnsAsyncQueue>(SnsAsyncMgr.queue);
		}
		private static void trySendSnsOpLog()
		{
			if (SnsAsyncMgr.curOpList.get_Count() > 0)
			{
				return;
			}
			if (SnsAsyncMgr.queue.OpList.get_Count() <= 0)
			{
				return;
			}
			SnsAsyncMgr.curOpList.AddRange(SnsAsyncMgr.queue.OpList);
			SnsAsyncMgr.sceneOp.doScene(SnsAsyncMgr.curOpList);
		}
		private static void onSendSnsOpLogRsp(IList<int> resultList)
		{
			if (resultList == null)
			{
				SnsAsyncMgr.curOpList.Clear();
				return;
			}
			if (SnsAsyncMgr.curOpList.get_Count() > 0)
			{
				if (resultList.get_Count() <= 0 || resultList.get_Count() == SnsAsyncMgr.curOpList.get_Count())
				{
					if (resultList.get_Count() > 0)
					{
						List<SnsOpLog> list = Enumerable.ToList<SnsOpLog>(Enumerable.Where<SnsOpLog>(SnsAsyncMgr.curOpList, (SnsOpLog snsOpLog_0, int index) => resultList.get_Item(index) != 0));
						if (list.get_Count() > 0)
						{
							using (List<SnsOpLog>.Enumerator enumerator = list.GetEnumerator())
							{
								while (enumerator.MoveNext())
								{
									SnsOpLog current = enumerator.get_Current();
									current.nRetryTimes++;
									if (current.nRetryTimes > 10)
									{
										SnsAsyncMgr.queue.OpList.Remove(current);
									}
								}
							}
							List<SnsOpLog> list2 = Enumerable.ToList<SnsOpLog>(Enumerable.Except<SnsOpLog>(SnsAsyncMgr.curOpList, list));
							SnsAsyncMgr.updateSnsObject(list2);
							SnsAsyncMgr.queue.OpList = Enumerable.ToList<SnsOpLog>(Enumerable.Except<SnsOpLog>(SnsAsyncMgr.queue.OpList, list2));
							ConfigMgr.delayWrite<SnsAsyncQueue>(SnsAsyncMgr.queue);
							SnsAsyncMgr.curOpList.Clear();
							return;
						}
					}
					SnsAsyncMgr.updateSnsObject(SnsAsyncMgr.curOpList);
					SnsAsyncMgr.queue.OpList = Enumerable.ToList<SnsOpLog>(Enumerable.Except<SnsOpLog>(SnsAsyncMgr.queue.OpList, SnsAsyncMgr.curOpList));
					ConfigMgr.delayWrite<SnsAsyncQueue>(SnsAsyncMgr.queue);
					SnsAsyncMgr.curOpList.Clear();
					SnsAsyncMgr.trySendSnsOpLog();
					return;
				}
			}
			DebugEx.debugBreak();
			SnsAsyncMgr.curOpList.Clear();
		}
		private static void updateSnsObject(List<SnsOpLog> opList)
		{
			using (List<SnsOpLog>.Enumerator enumerator = opList.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SnsOpLog current = enumerator.get_Current();
					if (current.nUpdateSnsObject != 0)
					{
						if (current.nOpType == 1)
						{
							SnsInfoMgr.smethod_1(current.nObjectID);
							SnsMsgMgr.deleteMsg(current.nObjectID);
						}
						else
						{
							SnsInfo snsInfo = SnsInfoMgr.smethod_0(current.nObjectID);
							if (snsInfo != null)
							{
								switch (current.nOpType)
								{
								case 2:
									snsInfo.setPrivacy(true);
									break;
								case 3:
									snsInfo.setPrivacy(false);
									break;
								case 4:
									snsInfo.deleteSnsComment(current.nDelCommentId);
									break;
								case 5:
									snsInfo.setLikeFlag(false, null);
									break;
								}
								StorageMgr.snsInfo.modify(snsInfo);
							}
						}
					}
				}
			}
		}
	}
}
